package org.wmine.eventhandlers;



import org.wmine.algo.AlgorithmPanel;
import org.wmine.core.BasicTabPanel;
import org.wmine.core.IProjectManager;
import org.wmine.core.FileBrowser;
import org.wmine.core.InvalidFileException;
import org.wmine.core.InvalidProjectFileException;
import org.wmine.core.MainWindow;
import org.wmine.core.ProjectListener;
import org.wmine.core.ProjectManager;
import org.wmine.core.IndexParserGenerator;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JFileChooser;






public class FileMenuEventHandler implements ActionListener {

	IProjectManager projMan;
	public void actionPerformed(ActionEvent actionEvent) {
		String subMenuName = actionEvent.getActionCommand();
		FileBrowser openFileDialog = new FileBrowser();
		projMan=ProjectManager. getUniqueInstance();
		if(subMenuName.equals("Text File")){
			openFileDialog.createOpenFileDialog(MainWindow.getInstance());
			//openFileDialog.
		}	
		else if(subMenuName.equals("Database File")) {
			ImportDatabaseEventHandler importDatabaseEventHandler = new ImportDatabaseEventHandler(MainWindow.getInstance());
//			openFileDialog.createOpenFileDialog(MainWindow.getInstance());
		}	
		else if(subMenuName.equals("Open Existing Project")) {
			JFileChooser fc=new JFileChooser();
			int result =  fc.showOpenDialog(null);
			//fc.setFileFilter();
			 StringBuffer title=new StringBuffer("WMine - Data Mining Tool");
			 
			 
			File file = null;
			String str;
			char chr[] = null;
			if (result == JFileChooser.APPROVE_OPTION)
			{
				file =  fc.getSelectedFile();


				System.out.println(fc.getSelectedFile());

			}
			try {
				StringBuffer fileChosen=new StringBuffer(fc.getSelectedFile().toString());
				projMan.openProject(fileChosen.toString());
				title.append(" ");
				StringBuffer onlyFname=new StringBuffer(fileChosen);
				onlyFname.delete(0,(new File(fileChosen.toString()).getParent().toString()).length());
				title.append("["+onlyFname.delete(onlyFname.length()-4,onlyFname.length())+"]");
				
			} catch (InvalidFileException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvalidProjectFileException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch(NullPointerException e)
			{
				//e.printStackTrace();
			}

		}	
		else if(subMenuName.equals("New Project")) {
			//openFileDialog.CreateSaveDialog();
			JFileChooser jc=new JFileChooser();
			String fileChosen=null;
			jc.showSaveDialog(null);
		
			try{
				fileChosen=jc.getSelectedFile().toString();
			}catch(NullPointerException e){}
			if(fileChosen!=null)
			{StringBuffer onlyFname=new StringBuffer(fileChosen);
			onlyFname.delete(0,(new File(fileChosen).getParent().toString()).length());
			//onlyFname.setLength((new File(fileChosen).getParent().toString()).length());
			
			projMan.createProject(onlyFname.toString(),new File(fileChosen).getParent().toString());
			}
		}
		else if(subMenuName.equals("Close Project")){
		//	System.out.println("in Filemenu eventhandler Closing Project");
					projMan.closeProject();
		}
		else if(subMenuName.equals("Exit"))
			System.exit(0);
	}	
}
